﻿package
{
	import org.flixel.*;
	
	public class Player extends FlxSprite
	{
		[Embed(source="data/player.png")] private var ImgPlayer:Class;
		[Embed(source="data/salto.mp3")] private var SoundJump:Class;
		[Embed(source="data/golpeplayer.mp3")] private var SoundHurt:Class;
		
		public var dying:Boolean;
		public var atackstate:uint;
		public var invencibility:Number;
		public var jumptime:Number;
		public var doubleJumpActivated:Boolean;
		public var doubleJump:Number;
		public var wallJump:Number;
		public var firing:Boolean;
		
		public static const STATE_BEFOREATACK1:uint  = 0;
		public static const STATE_ATACK1:uint  = 1;
		public static const STATE_BEFOREATACK2:uint  = 2;
		public static const STATE_ATACK2:uint  = 3;


		

		public function Player(X:Number, Y:Number)
		{
			super(X, Y);
			
			loadGraphic(ImgPlayer,true,true,45,35);
			maxVelocity.x = 100;			//walking speed
			acceleration.y = 400;			//gravity
			drag.x = maxVelocity.x*4;		//deceleration (sliding to a stop)
			
			health = 5;
		
			//tweak the bounding box for better feel
			width = 8;
			height = 23;
			offset.x = 17;
			offset.y = 6;
			
			dying = false;
			atackstate = STATE_BEFOREATACK1;
			invencibility = 0;
			doubleJumpActivated = true;
			doubleJump = 1;
			wallJump = 0;
			
			addAnimation("idle",[0],0,false);
			addAnimation("walk",[1,2,3,4,5],10,true);
			addAnimation("flail",[2],18,false);
			addAnimation("jump",[4],0,false);
			addAnimation("atack1",[7,8,9],7,false);
			addAnimation("atack2",[10,11,12],8,false);
			addAnimation("die",[6],10,false);
			addAnimation("fire",[0],10,false);
			}
		
		override public function update():void
		{
			//Smooth slidey walking controls
			if(dying){
				play("die");
				if(frame == 6)
					super.kill();
				return
			}
			if(frame == 9)
			 	atackstate = STATE_BEFOREATACK2;
			if(frame == 12)	
				atackstate = STATE_BEFOREATACK1;
			
		  	if((atackstate != STATE_ATACK1) && (atackstate != STATE_ATACK2))
		   	{
				acceleration.x = 0;
				if(FlxG.keys.LEFT || FlxG.keys.A)
					{acceleration.x -= drag.x;
				 	facing = LEFT;}
				if(FlxG.keys.RIGHT || FlxG.keys.D)
					{acceleration.x += drag.x;
				 	facing = RIGHT;}
				if(isTouching(FLOOR))
				{
					if(FlxG.keys.justPressed("Z") || FlxG.keys.justPressed("NUMPADONE"))
					    {
						jumptime = 0;
						doubleJump = 1;
						//FlxG.play(SoundJump);
						}
					else if((velocity.x != 0))
						play("walk");
					else if(finished)
						play("idle");
				}
				else if(velocity.y < 0)
					{
					play("jump");
					}
				else if(velocity.y > 0)
					play("flail");
				
				if(velocity.y != 0)
				    {
					if( (isTouching(WALL) || justTouched(WALL)) && (!(isTouching(FLOOR))) )
					   {wallJump = 1;doubleJump = 1;}
					else wallJump = 0;
					
					if(FlxG.keys.justReleased("Z") || FlxG.keys.justReleased("NUMPADONE"))
						{
						if(!doubleJumpActivated || doubleJump == 0) jumptime = 6;
						if((doubleJumpActivated && doubleJump)|| wallJump) {jumptime = 0;doubleJump = 0}
						}
					}
				}
				if(FlxG.keys.Z || FlxG.keys.NUMPADONE)
					    {
							if(jumptime < 6)
							   {
								if(velocity.y > 0)velocity.y = 0;   
								if(velocity.y >= -acceleration.y*0.51)
								   velocity.y -= acceleration.y*0.1
								if(wallJump)
									{
										if(isTouching(LEFT))velocity.x += drag.x*0.3
										if(isTouching(RIGHT))velocity.x -= drag.x*0.3
									}
   								jumptime++;
							   }
						}
				if(FlxG.keys.justPressed("X") || FlxG.keys.justPressed("NUMPADTWO"))
				{
						if(atackstate == STATE_BEFOREATACK1)
						{
						  atackstate = STATE_ATACK1;
	  					  play("atack1");
						}
						else if(atackstate == STATE_BEFOREATACK2)  
						{
						  atackstate = STATE_ATACK2;
  						  play("atack2");
						}
				}
				if(FlxG.keys.justPressed("C") || FlxG.keys.justPressed("NUMPADTHREE"))
				{
						play("fire");
						firing = true;
					    atackstate = STATE_BEFOREATACK1;
				}
				if(FlxG.keys.justReleased("C") || FlxG.keys.justReleased("NUMPADTHREE"))
						firing = false;
						
				

				if(invencibility > 0)
				{
					invencibility--;
					if(invencibility == 0)visible = true;
					else if([1,5,9,13,17].indexOf(invencibility) != -1)visible = false;
					else if([3,7,11,15,19].indexOf(invencibility) != -1)visible = true;
				}
				
				
		  	
		}
		
		override public function hurt(Damage:Number): void
		{
			var i:uint;
			
			if(Damage > 0)
			if(invencibility == 0){
				FlxG.play(SoundHurt);
				super.hurt(Damage);
				invencibility = 20;
				if(facing == RIGHT)
				{
					for(i = 0; i<3 ; i++)
						 velocity.x -= 10;
				}
				else if(facing == LEFT)
				{
					for(i = 0; i<3 ; i++)
						 velocity.x += 10;
				}
			}
		}
		
		
		
		override public function kill():void
		{
			dying = true;
		}
		
	}
}
